import axios from 'axios' 
class Api {
    request = axios.create({
      baseURL: '/api',
      timeout: 60*1000
    }) 
    /**
     * 构造方法
     * @param {*} arg
     */
    constructor (arg) {
      this.request.interceptors.request.use(config => { 
        return config
      }, error => {
        return Promise.reject(error)
      })
      this.request.interceptors.response.use(response => { 
        let status = response.status
        return response
      }, error => {
        if (error.response) {
          switch (error.response.status) {
            case 401:
              // 返回 401 清除token信息并跳转到登录页面
              console.log(error);
          }
        }
        return Promise.reject(error)
      })
    }

    get (url) {
      return this.request.get(url).then(res => res.data)
    }
    getByParam(url,param){
      return this.request.get(url,{params:param}).then(res => res.data)
    }
    post (url, param) {
      return this.request.post(url, param).then(res => res.data)
    }
    postByParam(url,param){
      return this.request.post(url,null,{params:param}).then(res=>res.data)
    }
    postByBody(url,data){
      return this.request.post(url,null,{data:data}).then(res=>res.data)
    }
    put(url,param){
      return this.request.put(url,param).then(res=>res.data)
    }
    putByParam(url,param){
      return this.request.put(url,null,{params:param}).then(res=>res.data)
    }
    delete(url){
      return this.request.delete(url).then(res=>res.data)
    }
    getTeamsByPage(param){
        return this.getByParam('/team/page-list',param)
    }
    updateTeam(param){
        return this.postByParam('/team/modify',param)
    }
    getPlayersByPage(param){
        return this.getByParam('/player/page-list',param)
    }
    updatePlayer(param){
        return this.postByParam('/player/modify',param)
    }
    getLogListByPage(param){
        return this.getByParam('/modify-log/page-list',param)
    }
    getDrawResult(param){
        return this.getByParam('/draw/show',param)
    }
    login(param){
      return this.postByParam("/login",param)
    }
    loggedIn(username){
      return this.get("/user/getUserInfo")
    } 
    testPost(param){
      return this.postByParam("/dev/test/test",param)
    } 
    getProcessDefinitionList(){
      return this.get("/processDefinition/getDefinitions")
    }
    postBPMNAndDeployment(bpmnContent,processName ){
      return this.postByBody("/processDefinition/postBPMNAndDeployment",{bpmnContent:bpmnContent,processName:processName})
    }
    getBPMNXML(deploymentId,resourceName){ 
      return this.getByParam("/processDefinition/getProcessDefineXML",{deploymentId:deploymentId,resourceName:resourceName})
    } 
    startProcessInstance(processDefinitionKey,instanceName){
        let param = {
          processDefinitionKey:processDefinitionKey,
          instanceName:instanceName
        }
        return this.postByParam("/processInstance/startProcess",param)
    } 
    getInstances(){
      return this.get("/processInstance/getInstances")
    }
    getTasks(){
      return this.get("/task/getTasks")
    }
    deleteDefined(deploymentId){
      return this.delete("/processDefinition/deleteDefined/"+deploymentId)
    }
    deleteInstance(instanceId){
      return this.delete("/processInstance/deleteProcess/"+instanceId)
    } 
    uploadFileAndDeployment(processName,processFile){
        let param = new FormData() 
        param.set("processName",processName)
        param.set("processFile",processFile)
        return this.post("/processDefinition/uploadFileAndDeployment",param)
    }
    suspendedInstance(instanceId){ 
         return this.post("/processInstance/suspendInstance/"+instanceId)
    }
    resumeInstance(instanceId){ 
      return this.post("/processInstance/resumeInstance/"+instanceId)
    } 
    completeTask(taskId){
      return this.post("/task/completeTask/"+taskId)
    }

}

const api = new Api()
export default api